package com.tvein.sanguo.controller.Game.user
{
	//flash
	import flash.external.ExternalInterface;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import com.tvein.sanguo.common.CommonConstants;
	import com.tvein.sanguo.model.SFSProxy;
//
	public class GameCommand extends SimpleFabricationCommand implements ICommand
	{
		private var sfsData:SFSProxy;
		
		override public function execute(notification:INotification) : void
		{
			sfsData = facade.retrieveProxy(SFSProxy.NAME) as SFSProxy;
			trace(notification.getBody()[1]);
			//检查命令格式
			var cmds:Array = String(notification.getBody()[1]).split(";");
			var cmdjso:Object = new Object;
			
			for (var i:int = 0; i < cmds.length; i++)
			{
				var func:String = cmds[i].substr(0, cmds[i].indexOf("(",0)).replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2");
				var params:Array = cmds[i].substring(cmds[i].indexOf("(", 0) + 1, cmds[i].indexOf(")", 0)).split(",");
				
				for (var j:int = 0; j < params.length; j++)
				{
					cmdjso["param" + j] = params[j].replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2");
				}
				
				switch (func)
				{
					//game
					case "GameExit":
						sfsData.sfs.disconnect();
						routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", false]], null, "*");
						routeNotification(CommonConstants.MODULE_INVOKE, ["-clear"], null, CommonConstants.MODULE_ROOM + "/*");
						break;
					case "ExchangePoint":
						sfsData.send("ExchangePoint", cmdjso);
						break;
					case "RoomChange":
						sfsData.send("RoomChange", cmdjso);
						routeNotification(CommonConstants.MODULE_INVOKE, ["-out"], null, CommonConstants.MODULE_BACKGROUND + "/*");
						routeNotification(CommonConstants.MODULE_INVOKE, ["-clear"], null, CommonConstants.MODULE_CROWD + "/*");
						break;
					case "GetGameData":
						sfsData.send("GameDataGet", cmdjso);
						break;
					case "MailHandle":
						if (cmdjso["param0"] == "2")
						{
							cmdjso["param1"] = unescape(cmdjso["param1"]);
							cmdjso["param2"] = unescape(cmdjso["param2"]);
							cmdjso["param3"] = unescape(cmdjso["param3"]);
						}

						sfsData.send("MailHandle", cmdjso);
						break;
					case "RoleCall":
						if (cmdjso["param1"] == "RoleChat")
						{
							cmdjso["param3"] = unescape(cmdjso["param3"]);
						}
						sfsData.send("RoleCall", cmdjso);
						break;
					//event
					case "MissionCall":
						sfsData.send("MissionCall", cmdjso);
						break;
					case "CommandCall":
						sfsData.send("CommandCall", cmdjso);
						break;
					case "ItemCall":
						sfsData.send("ItemCall", cmdjso);
						break;
					case "SkillCall":
						sfsData.send("SkillCall", cmdjso);
						break;
					case "RoomCall":
						sfsData.send("RoomCall", cmdjso);
						break;
					case "NextEvent":
						routeNotification(CommonConstants.MODULE_INVOKE, ["-run", ["play"]], null, CommonConstants.MODULE_EVENT + "/*");
						break;
				}
			}
		}
		
		//Custom
	}
}